package safari.model.message;

import java.sql.SQLException;
import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;

public class MessageDaoImpl implements MessageDao {
	private SqlSessionTemplate sqlSessionTemplate;
	public MessageDaoImpl(SqlSessionTemplate sqlSessionTemplate){
		this.sqlSessionTemplate=sqlSessionTemplate;
	}
	/**
	 * result== -1 : receiverId 가 존재하지 않는 것
	 * result== 0: receiverId 는 존재하는 id 이지만 쪽지 보내기 실패한 것
	 * result== 1 : 쪽지가 제대로 보내진 것
	 */
	@Override
	public int newMessage(MessageVO msgvo) throws SQLException {
		int result=-1;
		//isExist == 0 이면 receiverId 가 존재하지 않는 것
		int isExist=sqlSessionTemplate.selectOne("checkReceiver",msgvo.getReceiverId());
		if(isExist==1){
			result=sqlSessionTemplate.insert("message.newMessage",msgvo);
		}
		return result;
	}

	//받은메세지 리스트 가져오기
	@Override
	public List<MessageVO> getReceivedMessageList(IdAndPageVO ipvo)
			throws SQLException {
		return sqlSessionTemplate.selectList("message.getReceivedMessageList",ipvo);
	}

	@Override
	public List<MessageVO> getSendedMessageList(IdAndPageVO ipvo)
			throws SQLException {
		// TODO Auto-generated method stub
		List list=sqlSessionTemplate.selectList("message.getSendedMessageList",ipvo);
		System.out.println("여기 dao: "+list);
		return list;
	}

	@Override
	public int getNumberOfReceivedMessage(String myId) throws SQLException {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("message.getNumberOfReceivedMessage",myId);
	}

	@Override
	public int getNumberOfSendedMessage(String myId) throws SQLException {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("message.getNumberOfSendedMessage",myId);
	}
	
	/**
	 * 선택한 쪽지 자세히 보기
	 */
	@Override
	public MessageVO viewOneMessage(int no) throws SQLException {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("message.viewOneMessage",no);
	}
	@Override
	public int deleteMessage(int no) throws SQLException {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.delete("message.deleteMessage",no);
	}



}
